package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.entity.Doctor;
import com.example.entity.Posts;
import com.example.entity.vo.postVO;
import com.example.service.IDoctorService;
import com.example.service.IPostsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 问诊记录 前端控制器
 * </p>
 *
 * @author wyx
 * @since 2025-03-19
 */
@CrossOrigin//跨域访问
@Slf4j
@RestController
@Api(tags = "问诊记录管理模块")
@RequestMapping("/post")
public class PostsController {
    @Autowired
    IPostsService postsService;
    @Autowired
    IDoctorService doctorService;

    @ApiOperation(value = "分页查询问诊记录列表")
    @GetMapping("/page")
    public R page(
            @RequestParam(required = false) String postsContent,
            @RequestParam(required = false) Integer publisher,
            @RequestParam(required = false) Integer registrationId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize) {

        log.info("根据条件分页查询问诊记录, 内容: {}, 发布者ID: {}, 挂号帖ID: {}, 页码: {}, 每页大小: {}", postsContent, publisher, registrationId, page, pageSize);

        // 创建分页对象
        Page<Posts> pageInfo = new Page<>(page, pageSize);
        Page<postVO> pageInfo1 = new Page<>(page, pageSize);

        // 创建查询条件
        LambdaQueryWrapper<Posts> queryWrapper = new LambdaQueryWrapper<>();

        // 如果提供了内容参数，则添加内容查询条件
        if (postsContent != null && !postsContent.isEmpty()) {
            queryWrapper.like(Posts::getPostsContent, postsContent);
        }

        // 如果提供了发布者ID参数，则添加发布者ID查询条件
        if (publisher != null) {
            queryWrapper.eq(Posts::getPublisher, publisher);
        }

        // 如果提供了挂号帖ID参数，则添加挂号帖ID查询条件
        if (registrationId != null) {
            queryWrapper.eq(Posts::getRegistrationId, registrationId);
        }

        // 执行分页查询
        postsService.page(pageInfo, queryWrapper);

        // 将分页查询结果转换为 PostsVo
        List<postVO> postsVoList = pageInfo.getRecords().stream()
                .map(posts -> {
                    postVO vo = new postVO();
                    BeanUtils.copyProperties(posts, vo);
//                    vo.setDoctorName(doctorService.getById(posts.getPublisher()).getName());
                    // 获取 Doctor 对象
                    Doctor doctor = doctorService.getById(posts.getPublisher());
                    // 检查 doctor 是否为 null
                    if (doctor != null) {
                        vo.setDoctorName(doctor.getName());
                    } else {
                        // 如果 doctor 为 null，可以设置一个默认值或抛出异常
                        vo.setDoctorName("Unknown Doctor");
                        // 或者 throw new RuntimeException("Doctor not found for ID: " + posts.getPublisher());
                    }
                    return vo;
                })
                .collect(Collectors.toList());

        // 设置分页信息和转换后的 records
        BeanUtils.copyProperties(pageInfo, pageInfo1, "records");
        pageInfo1.setRecords(postsVoList);

        // 返回结果
        return R.success(pageInfo1);
    }

    @ApiOperation(value = "新增问诊记录")
    @PostMapping("/add")
    public R addPosts( Posts posts) {
        log.info("新增问诊记录: {}", posts);
        postsService.save(posts);
        return R.success("新增问诊记录成功");
    }

    @ApiOperation(value = "删除问诊记录")
    @DeleteMapping("/delete/{id}")
    public R deletePosts(@PathVariable Integer id) {
        log.info("删除问诊记录, ID: {}", id);
        postsService.removeById(id);
        return R.success("删除问诊记录成功");
    }


    @ApiOperation(value = "修改问诊记录")
    @PutMapping("/update")
    public R updatePosts(postVO postVO) {
        log.info("修改问诊记录: {}", postVO);
        Posts posts = new Posts();
        BeanUtils.copyProperties(postVO, posts);
        postsService.updateById(posts);
        return R.success("修改问诊记录成功");
    }

}
